//
//  CYAlert.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/14.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "YJDAlert.h"
@interface YJDAlertController ()
@property (nonatomic, strong) UILabel *customLabel;
@property (nonatomic, assign) CGFloat defaultWidth;
@end
@implementation YJDAlertController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    for (UIView *view in [self.view subviews])
    {
        view.layer.cornerRadius = 5.0;
        view.layer.borderColor = [UIColor lightGrayColor].CGColor;
        view.layer.borderWidth = 0.3;
        view.layer.masksToBounds = YES;
        [view removeFromSuperview];
    }
    // Do any additional setup after loading the view, typically from a nib.
}

- (UILabel *)customLabel
{
    if (!_customLabel)
    {
        _customLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        _customLabel.bounds = CGRectMake(0.0, 0.0, 150, 40);
        _customLabel.center = CGPointZero;
        _customLabel.backgroundColor = [UIColor whiteColor];
        _customLabel.layer.cornerRadius = 5.0;
        _customLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _customLabel.layer.borderWidth = 0.3;
        _customLabel.layer.masksToBounds = YES;
        _customLabel.textAlignment = NSTextAlignmentCenter;
        [self yjd_setupCustomLabelText];
        [self.view addSubview:_customLabel];
    }
    return _customLabel;
}

- (CGFloat)defaultWidth
{
    return SCREEN_MIN_LENGTH - 60;
}

- (void)yjd_setupCustomLabelText
{
    //获取提示语的size
    UIFont *font = [UIFont systemFontOfSize:15 weight:UIFontWeightLight];
    CGSize size = [self.message sizeWithAttributes:@{NSFontAttributeName:font}];
    
    //如果提示语的size的宽度大于设定的默认
    if (size.width > self.defaultWidth)
    {
        CGRect rect  = [self.message yjd_getHeightWithWidth:self.defaultWidth withFont:[UIFont systemFontOfSize:15 weight:UIFontWeightLight]];
        _customLabel.bounds = CGRectMake(0.0, 0.0, self.defaultWidth, rect.size.height + 20);
        _customLabel.numberOfLines = 0;
    }
    else
    {
        _customLabel.bounds = CGRectMake(0.0, 0.0, ceil(size.width) + 40, 40);
    }
    
    NSMutableAttributedString *alertVCMessage = [[NSMutableAttributedString alloc] initWithString:self.message];
    
    [alertVCMessage addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, self.message.length)];
    
    [alertVCMessage addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, self.message.length)];
    
    _customLabel.attributedText = alertVCMessage;
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.customLabel.hidden = NO;
}
@end


@interface YJDAlert ()

@end

@implementation YJDAlert
+ (void)yjd_showWithTitle:(NSString *)title message:(NSString *)message inController:(UIViewController *)controller complete:(void(^)(void))completion{
    
    WS(controller)
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *enterBtn = [UIAlertAction actionWithTitle:@"确定"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                         if (completion)
                                                             completion();
                                                     }];
    [alertVC addAction:enterBtn];
    [weakSelf.view endEditing:YES];
    [weakSelf presentViewController:alertVC animated:YES completion:nil];
}

+ (void)yjd_showInWindowWithTitle:(NSString *)title message:(NSString *)message complete:(void(^)(void))completion{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *enterBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                         if (completion)
                                                             completion();
                                                     }];
    [alertVC addAction:enterBtn];
    UIViewController *currentVC = [self findPresentedViewController:nil];
    if (currentVC)
    {
        [currentVC presentViewController:alertVC animated:YES completion:nil];
    }
    
}

+ (void)yjd_showInWindowWithTitle:(NSString *)title message:(NSString *)message enterComplete:(void(^)(void))completion{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *enterBtn = [UIAlertAction actionWithTitle:@"非常确定" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                         if (completion)
                                                             completion();
                                                     }];
    UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"我再想想" style:UIAlertActionStyleCancel
                                                      handler:nil];
    [alertVC addAction:enterBtn];
    [alertVC addAction:cancelBtn];
    UIViewController *currentVC = [self findPresentedViewController:nil];
    if (currentVC)
    {
        [currentVC presentViewController:alertVC animated:YES completion:nil];
    }
    
}

+ (void)yjd_showMessage:(NSString *)message hidden:(void(^)(void))hidden{
    
    YJDAlertController *alertVC = [YJDAlertController alertControllerWithTitle:nil
                                                                       message:message
                                                                preferredStyle:UIAlertControllerStyleAlert];
    //修改message
    NSMutableAttributedString *alertVCMessage = [[NSMutableAttributedString alloc] initWithString:message];
    //        [alertVCMessage addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];
    [alertVCMessage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15 weight:UIFontWeightLight] range:NSMakeRange(0, message.length)];
    [alertVC setValue:alertVCMessage forKey:@"attributedMessage"];
    
    
    UIViewController *currentVC = [self findPresentedViewController:nil];
    if (currentVC)
    {
        [currentVC presentViewController:alertVC animated:YES completion:^{
            dispatch_time_t delaytime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) 1.3 *NSEC_PER_SEC);//1.3秒
            dispatch_after(delaytime, dispatch_get_main_queue(), ^{
                alertVC.view.hidden = YES;
                [alertVC dismissViewControllerAnimated:NO completion:^{//1.3秒后，返回
                    if (hidden)
                    {
                        hidden();
                    }
                }];
            });
        }];
    }
}

+ (UIViewController*)findPresentedViewController:(UIViewController *)VC
{
    if (VC == nil)
    {
        UIViewController *newVC = [UIApplication sharedApplication].delegate.window.rootViewController;
        return [self findPresentedViewController:newVC];
    }
    else if (VC.presentedViewController)
    {
        return [self findPresentedViewController:VC.presentedViewController];
    }
    else if ([VC isKindOfClass:NSClassFromString(@"YJDAlertController")])
    {
        return nil;
    }
    else
    {
        [VC.view endEditing:YES];
        return VC;
    }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
